<template>
	<view class="sendSubmitOrder">
		<u-navbar title="承运信息" :background="background" title-color="#FFF" back-icon-color="#fff"></u-navbar>
		<!-- <view class="sendSubmitOrder-title">派单统计</view> -->
		
		
		<!-- <view class="sendSubmitOrder-content" v-for="(item,index) in sendobj.ORDTB403List" :key="index"> -->
		<view class="sendSubmitOrder-content">
			
			<view class="sendSubmitOrder-content-top">
				<p>
					厂家：{{materInfo.ORMTB460CA00}}
				</p>
				<p>
					物料：{{ddMasgObj.ORMTB470CA07}}
				</p>
			</view>
			<view class="sendSubmitOrder-content-p">
				<!-- <view class="sendSubmitOrder-content-p-li">
					<text>单价</text>
					<view>***元</view>
					<view>{{ddMasgObj.ORMTB470CA08}}元</view>
				</view> -->
				<!-- <view class="sendSubmitOrder-content-p-li">
					<text>物料重量</text>
					<view>{{countWeight}}{{materInfo.settb109CA05||'吨'}}</view>
				</view> -->
				<view class="sendSubmitOrder-content-p-li">
					<text>配送地：</text>
					<view>{{materInfo.ORMTB460CA01}}</view>
				</view>
				<view class="sendSubmitOrder-content-p-li">
					<text>车数</text>
					<view>{{ddMasgObj.CARCOUNT}}车</view>
				</view>
				<!-- <view class="sendSubmitOrder-content-p-li">
					<text>物料金额</text>
					<view>***元</view>
					<view>{{ddMasgObj.ORMTB470CA09}}元</view>
				</view> -->
				<view class="sendSubmitOrder-content-p-li">
					<text>运费单价</text>
					<!-- <view>***元</view> -->
					<view>{{materInfo.ORMTB460CA09}}元 1吨（以实际卸货重量为准）</view>
				</view>
				<!-- <view class="sendSubmitOrder-content-p-li">
					<text>优惠前金额</text>
					<view>***元</view>
					<view>{{initorderCountMoney}}元</view>
				</view> -->
			</view>
			<!-- <view class="sendSubmitOrder-content-bottom">
				<text>预计优惠</text>
				<view>***元</view>
				<view>减{{differencemoney}}元</view>
			</view>
			<view class="sendSubmitOrder-content-bottom">
				<text>优惠后金额</text>
				<view>***元</view>
				<view>￥{{orderCountMoney}}元</view>
			</view> -->
		</view>
		<view class="zhujie">
			<p style="color:red">注：{{REMARK}}</p>
		</view>
		<view class="sendSubmitOrder-nav-bottom">
			<view class="sendSubmitOrder-nav-bottom-l">
				<view class="">
					需冻结物料保证金：
					<!-- <text v-if="AuthorizationOBJ.ORMTB417CA06==0">￥***</text> -->
					<text
						>￥{{ddMasgObj.SUM}}</text>
					<!-- <text v-if="AuthorizationOBJ.SETTB116CA05==0">￥***</text> -->
				</view>
				<!-- <text>卸货后解冻</text> -->
				<!-- <text>（预扣金额=订单金额*1.2）</text> -->
			</view>
			<view class="sendSubmitOrder-nav-bottom-r">
				<button type="default" @click="submitOrderTap">确认接单</button>
			</view>
		</view>
		<u-modal v-model="submitShow" :show-cancel-button="true" @confirm="submitConfirm">
			<view class="slot-content">
				订单预扣金额为<label v-if="AuthorizationOBJ.ORMTB417CA06==0">***</label><label
					v-else>{{ddMasgObj.SUM}}</label>元，{{msgJDBZ}}
			</view>
		</u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		Decimal
	} from '../../static/api/decimal.min.js'
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#00a1f1',
				},
				// erpInfo: JSON.parse(uni.getStorageSync('erpinfo')),
				sendobj: JSON.parse(uni.getStorageSync('sendobj')),
				materInfo: JSON.parse(uni.getStorageSync('materInfo')),
				userinfo: JSON.parse(uni.getStorageSync('userinfo')),
				ddMasgObj:{},
				initcountMoney: 0,
				countMoney: 0, //物料金额
				countWeight: 0,
				countCarnum: 0,
				AuthorizationOBJ: '',
				submitShow: false,
				navId: '',
				repeatSubmit: false,
				yhSwitch: true,
				REMARK:'',
				list: [{
						name: '自买',
						disabled: false
					},
					{
						name: '配送',
						disabled: false
					}

				],
				value: '自买',
				asaddresssInfo: '',
				Erptype: '',
				couponPrice: 0,
				couponIds: '',
				ykesdjr:0,
				msgJDBZ:'',
			}
		},
		watch: {
		},
		computed: {
			freightMoney() {
				var money = 0
				if (this.asaddresssInfo != '') {
					money = new Decimal(this.countWeight).mul(new Decimal(this.asaddresssInfo.ORMTB440CA21))
				}
				return money
			},
			orderCountMoney() {
				var money = 0
				// var yhjMoney = new Decimal(this.countMoney).sub(new Decimal(this.couponPrice))
				if (this.sendobj.ORMTB440CA08 < 5) {
					var money = new Decimal(this.countCarnum).mul(1500)
					// this.ykesdjr = money

				} else {

					if (this.yhSwitch == true) {
						// money = new Decimal(this.countCarnum).mul(new Decimal(20))
						// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
						// this.ykesdjr = money
						// money = new Decimal(money).add(new Decimal(this.countMoney))
						money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))
						money = new Decimal(money).sub(new Decimal(this.couponPrice))

					} else {
						// money = new Decimal(this.countCarnum).mul(new Decimal(20))
						// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
						// this.ykesdjr = money
						// money = new Decimal(money).add(new Decimal(this.countMoney))
						// if(this.countMoney<1500){
						// 	this.countMoney =1500
						// }
						money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))


					}
				}


				return money
			},
			differencemoney() {
				var yhjMoney = new Decimal(this.countMoney).sub(new Decimal(this.couponPrice))
				
				if (this.yhSwitch == true) {
					var money = new Decimal(this.initcountMoney).sub(new Decimal(yhjMoney))
				} else {
					var money = new Decimal(this.initcountMoney).sub(new Decimal(this.countMoney))
				}
				console.log(money)
				if(money<0){
					money=0
				}
				return money
			},
			initorderCountMoney() {
				var money = new Decimal(this.freightMoney).add(new Decimal(this.initcountMoney))
				return money
			},
			preorderCountMoney() {
				var money = 0
				var yhjMoney = new Decimal(this.countMoney).sub(new Decimal(this.couponPrice))
				if (this.userinfo.sign == 'CUS-1') {

					if (this.sendobj.ORMTB440CA08 < 5) {
						money = new Decimal(this.countCarnum).mul(1500)
						money = new Decimal(money).mul(new Decimal(1.2))
					} else {
						if (this.erpInfo.ORMTB401CA01 == 'BD') {
							// money = new Decimal(this.countCarnum).mul(new Decimal(20))
							// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
							money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))
							// money = new Decimal(this.countMoney).mul(new Decimal(1.2))
							money = new Decimal(money).mul(new Decimal(1.2))

						} else {
							console.log(this.freightMoney)
							// money = new Decimal(this.countCarnum).mul(new Decimal(20))
							// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
							// money = new Decimal(money).add(new Decimal(this.countMoney))

							money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))
						}
					}
				} else {
					if (this.sendobj.ORMTB440CA08 < 5) {
						money = new Decimal(this.countCarnum).mul(1500)
						money = new Decimal(money).mul(new Decimal(1.2))
					} else {
						if (this.yhSwitch == true) {
							// money = new Decimal(this.countCarnum).mul(new Decimal(20))
							// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
							// money = new Decimal(money).add(new Decimal(this.countMoney))
							money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))
							money = new Decimal(money).sub(new Decimal(this.couponPrice))
							console.log(money)
							money = new Decimal(money).mul(new Decimal(1.2))
						} else {
							// money = new Decimal(this.countCarnum).mul(new Decimal(20))
							// money = new Decimal(money).mul(new Decimal(this.sendobj.ORMTB440CA08))
							// money = new Decimal(money).add(new Decimal(this.countMoney))
							money = new Decimal(this.freightMoney).add(new Decimal(this.countMoney))
							money = new Decimal(money).mul(new Decimal(1.2))
						}
					}




				}
				money = Number(money.toFixed(2));
				money=Number(money.toString().match(/^\d+(?:\.\d{0,2})?/));
				return money
			}
		},
		onShow() {
			
			// this.numMoneyfn('no')
			// this.getRestultFromServer()
			
			this.queryddMsag()
			this.querJDBZMsag()

		},

		methods: {
			switch1Change(e) {
				this.yhSwitch = e.detail.value
				console.log(this.yhSwitch)
				if (this.yhSwitch == true) {
					this.getRestultFromServer()
				}
			},
			// switch打开或者关闭时触发，值为true或者false

			getRestultFromServer() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/set/settb409/queryCouponPrice',
					method: 'POST',
					dataType: 'json',
					data: {
						"ORMTB410ID": this.sendobj.ORMTB410ID, //二级客户iD
						"ORDTB400CA08": this.countMoney,
						// "ORMTB400ID": this.sendobj.ORMTB400ID, //一级客户ID
						"ORMTB440CA23": this.sendobj.ORDTB400CA09 //二级企业id
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.couponPrice = res.data.data.couponPrice
							this.couponIds = res.data.data.couponIds

						} else {
							// this.numMoneyfn('no')
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						this.numMoneyfn('no')
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});


				// setTimeout(() => {
				// 	this.controlStatus = true;
				// 	// 后端允许用户关闭switch，设置checked为false，结束loading状态
				// 	this.loading = false;
				// 	this.yhSwitch = false;
				// }, 1500);
			},
			numMoneyfn(type) {
				var permoney = 0
				if (type != 'no') {
					var preferential = JSON.parse(type.SETTB400CA06)
					if (type.SETTB400CA02 == '0202') {
						permoney = new Decimal(100).sub(new Decimal(preferential['0402']))
						permoney = new Decimal(permoney).div(new Decimal(100))
					}
				}
				this.sendobj.carList.forEach(item => {
					console.log(item)
					// var jesyk = 20
					// var jesyknum = new Decimal(item.ORDTB400CA07).sub(new Decimal(jesyk))
					// console.log(jesyknum)
					// if(jesyknum<20){
					// 	item.money = new Decimal(this.sendobj.ORMTB440CA08).mul(new Decimal(20))
					// 	item.initCarmoney = new Decimal(this.sendobj.ORMTB440CA08).mul(new Decimal(20))
					// }
					if(item.money<1500){
						item.money =1500
					}
					var initCarmoney = new Decimal(item.initCarmoney).mul(new Decimal(item.ORDTB404CA03))
					this.initcountMoney = new Decimal(initCarmoney).add(new Decimal(this.initcountMoney))
					this.initcountMoney = Number(this.initcountMoney.toString().match(/^\d+(?:\.\d{0,2})?/));
					if (type == 'no' || type.SETTB400CA02 != '0202') {
						var moneynum = new Decimal(item.money).mul(new Decimal(item.ORDTB404CA03))
						this.countMoney = new Decimal(moneynum).add(new Decimal(this.countMoney))
						this.countMoney = Number(this.countMoney.toString().match(/^\d+(?:\.\d{0,2})?/));
					} else {
						if (type.SETTB400CA02 == '0202') {
							var moneynum = new Decimal(item.money).mul(new Decimal(permoney))
							moneynum = Number(moneynum.toFixed(2));
							moneynum = new Decimal(moneynum).add(new Decimal(preferential['0403']))
							moneynum = new Decimal(moneynum).mul(new Decimal(item.ORDTB404CA03))
							var countmoney = new Decimal(item.money).mul(new Decimal(item.ORDTB404CA03))
							moneynum = new Decimal(countmoney).sub(new Decimal(moneynum))
							this.countMoney = new Decimal(moneynum).add(new Decimal(this.countMoney))
							this.countMoney = Number(this.countMoney.toString().match(/^\d+(?:\.\d{0,2})?/));
						}
					}
					if (this.sendobj.ORDTB400CA06 == 1) {
						var weightnum = new Decimal(item.ORDTB400CA07).sub(new Decimal(item.tareMo))
					} else {
						var weightnum = item.ORDTB400CA07
					}
					weightnum = new Decimal(weightnum).mul(new Decimal(item.ORDTB404CA03))
					
					// if(weightnum<20){
					// 	weightnum = 20
					// }
					console.log(weightnum)
					this.countWeight = new Decimal(weightnum).add(new Decimal(this.countWeight))
					this.countWeight = Number(this.countWeight.toString().match(/^\d+(?:\.\d{0,2})?/));
					this.countCarnum = new Decimal(this.countCarnum).add(new Decimal(item.ORDTB404CA03))
					this.countCarnum = Number(this.countCarnum.toString().match(/^\d+(?:\.\d{0,2})?/));
					
				})
			},
			addressTap() {
				var str = JSON.stringify(this.asaddresssInfo)
				uni.navigateTo({
					url: '/pages/SOdeliveryAddress/index?value=' + this.value + '&asaddresssInfo=' + str
				})
			},
			// 选中某个单选框时，由radio时触发
			RadioChange(e) {
				if (e == '自买') {
					this.asaddresssInfo = ''
				} else {
					if (uni.getStorageSync('obj')) {
						var obj = JSON.parse(uni.getStorageSync('obj'))
						this.asaddresssInfo = obj.asaddresssInfo
					}
				}
			},
			// 选中任一radio时，由radio-group触发
			radioGroupChange(e) {
				// console.log(e);
			},
			submitOrderTap() {
				if (this.repeatSubmit) {
					uni.showToast({
						title: '订单已提交请勿重复提交！！',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				this.sendobj.ORMTB440CF00 = this.value == '配送' ? 'ZG' : 'LS'
				if (this.value == '配送') {
					if (this.asaddresssInfo) {
						if (this.asaddresssInfo.ORMTB440CA14 == '') {
							uni.showToast({
								title: '请选择配送地！！',
								duration: 5000,
								icon: 'none',
							});
							this.repeatSubmit = false
							return
						}
						if (this.asaddresssInfo.ORMTB440CA16 == '') {
							uni.showToast({
								title: '请选择配送工地！！',
								duration: 5000,
								icon: 'none',
							});
							this.repeatSubmit = false
							return
						}
						this.sendobj.ORMTB440CA12 = this.asaddresssInfo.freightMoney
						this.sendobj.ORMTB440CA21 = this.asaddresssInfo.ORMTB440CA21
						this.sendobj.ORMTB440CA13 = this.asaddresssInfo.ORMTB440CA13
						this.sendobj.ORMTB440CA14 = this.asaddresssInfo.ORMTB440CA14
						this.sendobj.ORMTB440CA15 = this.asaddresssInfo.ORMTB440CA15
						this.sendobj.ORMTB440CA16 = this.asaddresssInfo.ORMTB440CA16
					} else {
						uni.showToast({
							title: '请选择配送地！！',
							duration: 5000,
							icon: 'none',
						});
						this.repeatSubmit = false
						return
					}

				} else {
					this.sendobj.ORMTB440CA21 = 0
				}
				this.submitShow = true
			},
			queryddMsag() { //查询订单详情
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb470/calcOrder',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB460ID: this.sendobj.ORMTB460ID, //需求iD
						ORMTB410ID: this.sendobj.ORMTB410ID, // 接单人ID
						carList: this.sendobj.carList //二级企业id
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							console.log(res.data.data)
							if (res.data.data) {
								this.ddMasgObj = res.data.data
								this.REMARK = res.data.data.REMARK
								// this.numMoneyfn(res.data.data)
							} else {
								// this.numMoneyfn('no')
							}
						} else {
							// this.numMoneyfn('no')
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						// this.numMoneyfn('no')
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			
			querJDBZMsag() { //查询接单取消时间备注
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb460/getRemark2',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB460ID: this.sendobj.ORMTB460ID, //需求iD
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							if (res.data.data) {
								this.msgJDBZ = res.data.data
							} 
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						// this.numMoneyfn('no')
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			
			queryAuthorization() { //查询子账户权限
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/selectAccess/' + this.userinfo.ORMTB417ID,
					method: 'GET',
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.AuthorizationOBJ = res.data.data
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			submitConfirm() {
				this.repeatSubmit = true
				uni.showLoading({
					title: '请求中...',
					mask: true
				});
				console.log(this.sendobj)
				let carList = []
				this.sendobj.carList.forEach((item, index) => {
					carList.push({
						carPlate: item.carPlate,
						weight: item.ORDTB400CA07
					})
				});
				console.log(carList)
				uni.request({
					url: this.baseUrl + '/orm/ormtb460/acceptOrmtb460UnCar',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB460ID: this.sendobj.ORMTB460ID, //需求iD
						ORMTB410CA00: this.sendobj.ORMTB410ID, // 接单人ID
						PERTB005ID: this.sendobj.PERTB005ID,
						ormtb461List: carList //二级企业id
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {

							if (this.Erptype == 3) {
								uni.reLaunch({
									url: '/pages/priceComparison/index'
								})
							} else {
								uni.redirectTo({
									url: '/pages/yunfeiMaseg/wdyfdd/index'
								})
							}
							uni.removeStorageSync('materInfo')
							uni.removeStorageSync('sendobj')
							uni.showToast({
								title: '接单成功',
								duration: 5000,
								icon: 'none',
							});
						} else {
							uni.showToast({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						this.repeatSubmit = false
						uni.hideLoading();
					},
					fail: (res) => {
						uni.showToast({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
						uni.hideLoading();
					}
				});
			},
		}
	}
</script>

<style scoped>
	.sendSubmitOrder {
		width: 100vw;
		min-height: 100vh;
		background-color: #e3e3e3;
	}

	.sendSubmitOrder .slot-content {
		padding: 20rpx 30rpx;
	}

	.sendSubmitOrder .slot-content label {
		color: red;
	}

	.sendSubmitOrder-sal-type {
		display: flex;
		background-color: #FFF;
	}

	.sendSubmitOrder-sal-type-text {
		height: 90rpx;
		line-height: 90rpx;
		padding: 0 10rpx 0 20rpx;
	}

	.sendSubmitOrder-sal-type-radio {
		margin-left: 40rpx;
		padding: 20rpx 0;
	}

	.sendSubmitOrder-address {
		display: flex;
		background-color: #FFF;
		margin-top: 10rpx;
		padding: 10px 0;
	}

	.sendSubmitOrder-address text {
		text-align: center;
	}

	.sendSubmitOrder-address .iconweizhi {
		width: 100rpx;
		display: flex;
		align-items: center;
		color: #007AFF;
		padding-left: 50rpx;
	}

	.sendSubmitOrder-address-info {
		display: flex;
		align-items: center;
	}

	.sendSubmitOrder-title {
		height: 70rpx;
		line-height: 70rpx;
		padding-left: 30rpx;
		color: #333;
		font-size: 30rpx;
		background-color: #FFF;
	}

	.sendSubmitOrder-content {
		margin-bottom: 60rpx;
		margin-top: 10rpx;
	}

	.sendSubmitOrder-content-top {
		display: flex;
		justify-content: space-between;
		height: 60rpx;
		line-height: 60rpx;
		padding-left: 30rpx;
		padding-right: 30rpx;
		color: #333;
		font-size: 36rpx;
		margin-bottom: 2rpx;
		background-color: #FFF;
	}

	.sendSubmitOrder-content-p {
		padding: 30rpx;
		background-color: #FFF;
	}

	.sendSubmitOrder-content-p-li {
		display: flex;
		justify-content: space-between;
		/* font-size: 26rpx; */
		color: #333;
	}

	.sendSubmitOrder-content-p-li+.sendSubmitOrder-content-p-li {
		margin-top: 30rpx;
	}

	.sendSubmitOrder-content-p-li text {
		color: #999;
	}

	.sendSubmitOrder-content-p-li view {
		flex: 1;
		text-align: right;
	}

	.sendSubmitOrder-content-bottom {
		display: flex;
		justify-content: space-between;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #FFF;
		font-size: 30rpx;
		color: #333;
		padding: 0 30rpx;
	}

	.sendSubmitOrder-content-bottom view {
		flex: 1;
		text-align: right;
		padding: 0 20rpx;
		color: red;
	}

	.sendSubmitOrder-nav-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		width: 100%;
		height: 120rpx;
		background-color: #FFF;
		z-index: 2;
		justify-content: space-between;
	}

	.sendSubmitOrder-nav-bottom-l {
		flex: 1;
		text-align: right;
		padding-right: 20rpx;
		margin-top: 20rpx;
		color: #333;
	}

	.sendSubmitOrder-nav-bottom-l view {
		/* font-size: 30rpx; */
	}

	.sendSubmitOrder-nav-bottom-l>view>text {
		color: red;
		font-size: 40rpx;
	}

	.sendSubmitOrder-nav-bottom-l>text {
		font-size: 24rpx;
		color: #999;
	}

	.sendSubmitOrder-nav-bottom-r button {
		height: 120rpx;
		line-height: 120rpx;
		background-color: #02a2f4;
		color: #FFF;
	}

	.zdy-ty-xz {
		color: #e3e3e3 !important;
		font-size: 14rpx;
	}
	.zhujie{
		padding: 0 20rpx;
	}
</style>
